/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "视频视口.h"
#include "框架/操作环境管理.h"

#include <公共函数.h>




static C_视频页面*		g当前视频视口 = 0;
//static C_属性栏面板*	g属性栏 = 0;
static bool g开始移动视口 = false;




static void f_属性栏面板_部件构建(C_属性栏* plane, S_结构对象指针 obj) {
	//S_Props	prop = f_alloc_OpsProp(f_Operator初始化跟踪点, obj);
	//plane->m_属性.push_back(prop);
}


static void f_Operator初始化跟踪点(S_结构对象指针 ops) {
	//C_节点面板* node = (C_节点面板*)ops;
	std::cout<<"开始初始化跟踪点"<<std::endl;
	C_流视频节点* 流视频节点 = ((C_流视频节点*)ops);

	if (流视频节点) {
		std::vector<S_Rect2Df> rects = 流视频节点->f_模板探测跟踪点({0, 10}, {0, 10});

		if (rects.size()) {
			std::vector<S_RGBA8UI> color(rects.size(), { 50, 255, 50, 255 });
			f_ui_图像视口填充捕捉点(g当前视频视口->m_视频页面, rects, color);
		}
	}
}










static bool g开启选取 = false;
static S_Rect2Df g选取 = {};
static S_RGBA8UI g选取颜色 = S_UI主题::uic_选取边;


static bool f_视频框鼠标按下(C_Widget* self, const E_鼠标按钮& 按钮) {

	if (按钮 == E_鼠标按钮::e_鼠标左键) {
		auto* view = self->m_父部件->m_扩展视口属性;

		vec2 coord = f_widget_get鼠标局部位置(C_Widget::g_鼠标.gPos, self);
		g选取.offset = f_Widget_getOriginal(coord - view->m_中心, view->m_比例);
		g开启选取 = true;
	}

	return true;
}

static bool f_视频框鼠标放开(C_Widget* self, const E_鼠标按钮& 按钮) {
	if (按钮 == E_鼠标按钮::e_鼠标左键) {
		//C_视频页面* 页面 = dynamic_cast<C_视频页面*>(self);
		auto* view = self->m_父部件->m_扩展视口属性;

		f_ui_图像视口_关闭选区(self);
		g开启选取 = false;

		if (g当前视频视口->m_绑定数据_流视频节点) {
			//g当前视频视口->m_绑定数据_流视频节点->f_模板探测跟踪点(g选取.offset, g选取.extent);
			vec2 w = { g选取.offset.x - view->m_中心.x, g选取.offset.x + g选取.extent.x - view->m_中心.x };
			vec2 h = { g选取.offset.y - view->m_中心.y, g选取.offset.y + g选取.extent.y - view->m_中心.y };
			w = f_Widget_getOriginal(w, view->m_比例);
			h = f_Widget_getOriginal(h, view->m_比例);
			if (w.y > 0 && h.y > 0) {
				std::vector<S_Rect2Df> rects = g当前视频视口->m_绑定数据_流视频节点->f_模板探测跟踪点(w, h);

				if (rects.size()) {
					std::vector<S_RGBA8UI> color(rects.size(), { 50, 255, 50, 255 });
					f_ui_图像视口填充捕捉点(self, rects, color);
				}
				
			}
			
		}
	}

	return true;
}

static bool f_视频框鼠标位置改变(C_Widget* self, const ivec4& mouse) {
	auto* view = self->m_父部件->m_扩展视口属性;

	vec2 pos{float32(mouse.x), float32(mouse.y)};
	vec2 coord = f_Widget_getOriginal(f_widget_get鼠标局部位置(pos, self) - view->m_中心, view->m_比例);

	if (g开启选取) {
		g选取.extent = coord - g选取.offset;
		f_ui_图像视口_设置选区(self, g选取, g选取颜色);
	}

	return true;
}













static bool f_视频页面鼠标按下(C_Widget* self, const E_鼠标按钮& 按钮) {

	if (按钮 == E_鼠标按钮::e_鼠标左键) {

	}
	else if (按钮 == E_鼠标按钮::e_鼠标中键) {
		g开始移动视口 = true;
	}

	return true;
}

static bool f_视频页面鼠标放开(C_Widget* self, const E_鼠标按钮& 按钮) {
	if (按钮 == E_鼠标按钮::e_鼠标左键) {

	}
	else if (按钮 == E_鼠标按钮::e_鼠标中键) {
		g开始移动视口 = false;
	}

	return true;
}

static bool f_视频页面鼠标位置改变(C_Widget* self, const ivec4& mouse) {
	
	vec2 globalLoc{ float32(mouse.x), float32(mouse.y)};
	static vec2 absLoc = globalLoc;
	if (g开始移动视口) {
		C_Widget* view = f_ui_get滚动框视口(self);

		absLoc = globalLoc - absLoc;

		self->m_扩展视口属性->m_中心.x += absLoc.x;
		self->m_扩展视口属性->m_中心.y += absLoc.y;

		f_Widget_应用视口移动(self, view);
	}

	absLoc = globalLoc;

	return true;
}

bool f_视频页面鼠标滚轮(C_Widget* self, const S_鼠标& 鼠标) {
	C_Widget* view = f_ui_get滚动框视口(self);

	float32 缩放量 = 1;
	if (鼠标.lZ > 0) {
		缩放量 = 1.3;
	}
	else if (鼠标.lZ < 0) {
		缩放量 = 0.7;
	}

	f_C_Widget_鼠标位置视口缩放(self, { 缩放量 ,缩放量 });
	//f_C_Widget_应用视口缩放(self, view);
	return true;
}



static void f_视频页面布局(C_Widget* r, C_Widget** w, uint32 num) {
	C_视频页面* 页面 = dynamic_cast<C_视频页面*>(r);

	页面->m_滚动框->f_setSize(页面->f_getSize());

	float32 高度 = f_Clamp(页面->m_属性栏->m_Size.y, 256, r->m_Size.y);
	if (页面->m_属性栏->m_Size.y != 高度) {
		页面->m_属性栏->f_setH(高度);

		
	} else {
		
	}
	//f_单个部件裁剪(页面->m_属性栏, 页面->m_滚动框->m_裁剪大小, 页面->m_滚动框->m_GlobalLoc);
	//节点视口->f_setSize(节点视口->m_父部件->m_Size);
}



static void f_视频页面动画(C_Widget* self) {
	if (self->m_被裁剪 == false) {
		C_视频页面* 页面 = dynamic_cast<C_视频页面*>(self);
		if (页面->m_绑定数据_流视频节点) {
			auto& out跟踪坐标 = *DEF_Rect32f_Array1DValue(页面->m_绑定数据_流视频节点->f_get输出插座(2), 0);

			if (out跟踪坐标.size()) {
				std::vector<S_RGBA8UI> color(out跟踪坐标.size(), { 255, 255, 25, 255 });
				f_ui_图像视口填充捕捉点(页面->m_视频页面, out跟踪坐标, color);
			}
		}
		
	}
	
}



C_视频页面::C_视频页面(S_UI创建环境& ctx) : C_Widget() {
	mf_布局Fun = f_视频页面布局;
	mf_动画更新 = f_视频页面动画;


	m_属性栏 = f_创建属性边栏(ctx);
	//m_属性栏 = f_取属性边栏(m_属性栏滚动窗口);
	{
		//m_属性栏->m_部件构建 = f_属性栏面板_部件构建;
		m_属性栏->f_setW(200);
		//m_属性栏滚动窗口->f_setW(200);
	}
	


	m_视频页面 = f_ui_创建图像视口(ctx);
	{
		m_视频页面->f_setOriginalSize({ 512, 512 });

		m_视频页面->mf_鼠标按键按下 = f_视频框鼠标按下;
		m_视频页面->mf_鼠标按键放开 = f_视频框鼠标放开;
		m_视频页面->mf_鼠标移动 = f_视频框鼠标位置改变;
	}
	




	m_滚动框 = f_ui_创建滚动框(ctx, true, true, m_视频页面);
	f_widget_开启视口属性(m_滚动框);

	m_滚动框->m_扩展视口属性->m_大小 = { 1024, 1024 };
	m_滚动框->mf_鼠标滚轮 = f_视频页面鼠标滚轮;
	m_滚动框->mf_鼠标按键按下 = f_视频页面鼠标按下;
	m_滚动框->mf_鼠标按键放开 = f_视频页面鼠标放开;
	m_滚动框->mf_鼠标移动 = f_视频页面鼠标位置改变;


	

	
	f_添加子组件(m_滚动框);
	f_添加子组件(m_属性栏);


	m_绑定数据_流视频节点 = 0;
	m_Ops特效提取 = f_alloc_Ops(f_Operator初始化跟踪点, (S_结构对象指针)this);

}

C_视频页面::~C_视频页面() {
}




C_Widget* f_图像View_创建视频窗口(S_UI创建环境& ctx) {
	C_视频页面* view = new C_视频页面(ctx);

	return view;
}






void f_图像View_设置当前视频视口(C_Widget* w) {
	g当前视频视口 = dynamic_cast<C_视频页面*>(w);

	

}



void f_图像View_设置显示纹理(S_纹理* tex, C_节点基类* node) {
	C_Widget* view = f_ui_get滚动框视口(g当前视频视口->m_滚动框);
	if (view) {
		f_ui_set图像视口纹理(view, tex, 0);
	}

	g当前视频视口->m_绑定数据_流视频节点 = dynamic_cast<C_流视频节点*>(node);

	//g当前视频视口->m_属性栏->f_绑定操作对象((S_结构对象指针)g当前视频视口);
	f_属性栏面板_部件构建(g当前视频视口->m_属性栏, (S_结构对象指针)view);

}


void f_图像View_设置操作视频(C_节点基类* node) {
	uint32 num = node->f_get输出插座Num();
	g当前视频视口->m_绑定数据_流视频节点 = dynamic_cast<C_流视频节点*>(node);


	if (g当前视频视口->m_绑定数据_流视频节点 && num > 1) {
		C_Widget* view = f_ui_get滚动框视口(g当前视频视口->m_滚动框);

		for (uint32 i = 1; i < num; ++i) {
			auto* socket = node->f_get输出插座(i);
			/*auto* tex = socket->f_get纹理();

			if (tex) {
				f_ui_set图像视口纹理(view, tex, 0);
			}*/
		}

	}

	
	//==================== 构建属性面板部件 ======================
	std::vector<S_Props> props;

	g当前视频视口->m_Ops特效提取->m_Func = f_Operator初始化跟踪点;
	g当前视频视口->m_Ops特效提取->m_Obj = (S_结构对象指针)(g当前视频视口->m_绑定数据_流视频节点);
	//S_Props	prop = f_alloc_OpsProp(g当前视频视口->m_Ops特效提取, L"特征坐标提取");
	//props.push_back(prop);

	
	S_Props	prop = f_alloc_I32Prop(&(g当前视频视口->m_绑定数据_流视频节点->m_阈值步长), L"阈值步长");
	props.push_back(prop);
	prop = f_alloc_I32Prop(&(g当前视频视口->m_绑定数据_流视频节点->m_起始阈值), L"起始阈值");
	props.push_back(prop);
	prop = f_alloc_I32Prop(&(g当前视频视口->m_绑定数据_流视频节点->m_终止阈值), L"终止阈值");
	props.push_back(prop);
	prop = f_alloc_I32Prop(&(g当前视频视口->m_绑定数据_流视频节点->m_重复的最小次数), L"重复的最小次数");
	props.push_back(prop);
	prop = f_alloc_I32Prop(&(g当前视频视口->m_绑定数据_流视频节点->m_最小的斑点距离), L"最小的斑点距离");
	props.push_back(prop);



	prop = f_alloc_BoolProp(&g当前视频视口->m_绑定数据_流视频节点->m_斑点颜色限制, L"斑点灰度限制");
	props.push_back(prop);
	prop = f_alloc_UI8Prop(&(g当前视频视口->m_绑定数据_流视频节点->m_斑点灰度), L"斑点灰度");
	props.push_back(prop);


	prop = f_alloc_BoolProp(&g当前视频视口->m_绑定数据_流视频节点->m_斑点面积限制, L"斑点面积限制");
	props.push_back(prop);
	prop = f_alloc_I32Prop(&g当前视频视口->m_绑定数据_流视频节点->m_斑点最小面积, L"斑点最小面积");
	props.push_back(prop);
	prop = f_alloc_I32Prop(&g当前视频视口->m_绑定数据_流视频节点->m_斑点最大面积, L"斑点最大面积");
	props.push_back(prop);


	prop = f_alloc_BoolProp(&g当前视频视口->m_绑定数据_流视频节点->m_斑点圆度限制, L"斑点圆度限制");
	props.push_back(prop);
	prop = f_alloc_F32Prop(&g当前视频视口->m_绑定数据_流视频节点->m_斑点最小圆度, L"斑点最小圆度");
	props.push_back(prop);
	prop = f_alloc_F32Prop(&g当前视频视口->m_绑定数据_流视频节点->m_斑点最大圆度, L"斑点最大圆度");
	props.push_back(prop);


	prop = f_alloc_BoolProp(&g当前视频视口->m_绑定数据_流视频节点->m_斑点惯性率限制, L"斑点惯性率限制");
	props.push_back(prop);
	prop = f_alloc_F32Prop(&g当前视频视口->m_绑定数据_流视频节点->m_斑点最小惯性率, L"斑点最小惯性率");
	props.push_back(prop);
	prop = f_alloc_F32Prop(&g当前视频视口->m_绑定数据_流视频节点->m_斑点最大惯性率, L"斑点最大惯性率");
	props.push_back(prop);


	prop = f_alloc_BoolProp(&g当前视频视口->m_绑定数据_流视频节点->m_斑点凸度限制, L"斑点凸度限制");
	props.push_back(prop);
	prop = f_alloc_F32Prop(&g当前视频视口->m_绑定数据_流视频节点->m_斑点最小凸度, L"斑点最小凸度");
	props.push_back(prop);
	prop = f_alloc_F32Prop(&g当前视频视口->m_绑定数据_流视频节点->m_斑点最大凸度, L"斑点最大凸度");
	props.push_back(prop);


	
	props.push_back(g当前视频视口->m_绑定数据_流视频节点->m_Prop伽马);
	props.push_back(g当前视频视口->m_绑定数据_流视频节点->m_Prop亮度);
	props.push_back(g当前视频视口->m_绑定数据_流视频节点->m_Prop对比度);




	g当前视频视口->m_属性栏->f_构建属性部件(props);

	g当前视频视口->m_绘制属性更新 = true;

}






